<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Questions_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('xlsimport/php-excel-reader/excel_reader2');
        $this->load->helper('xlsimport/spreadsheetreader.php');
        $this->load->model('qbank_model', '', true);
        $this->load->model('user', '', true);
        
        if (!$this->session->userdata('logged_in')) {
            redirect('login');
        }
        
        $this->LoggedUser = $this->user->getLoggedUser();
        
        $this->load->library('authentication');$this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();$this->authentication->redirectIfNotBe( array('SS','KAS'));
    }

    function index($limit = '0') {
//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }

        $this->load->helper('form');
        $this->load->model('category', '', TRUE);
        $this->load->model('difficult_level', '', TRUE);
        
        $viewData['category'] = $this->category->category_dropdown();
        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();
        $viewData['result'] = $this->qbank_model->question_list($limit);
        $viewData['title'] = "Question Bank";
        $viewData['limit'] = $limit;
        $viewData['LoggedUser'] = $this->LoggedUser;
        
        $this->load->view(get_folder($this->loggedUser->title).'/questions_list', $viewData);
        
    }

    function import() {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }
        if (isset($_FILES['xlsfile'])) {
            $targets = 'xls/';
            $targets = $targets . basename($_FILES['xlsfile']['name']);
            $docadd = ($_FILES['xlsfile']['name']);
            if (move_uploaded_file($_FILES['xlsfile']['tmp_name'], $targets)) {
                $Filepath = $targets;
                $allxlsdata = array();
                date_default_timezone_set('UTC');

                $StartMem = memory_get_usage();
                //echo '---------------------------------'.PHP_EOL;
                //echo 'Starting memory: '.$StartMem.PHP_EOL;
                //echo '---------------------------------'.PHP_EOL;

                try {
                    $Spreadsheet = new SpreadsheetReader($Filepath);
                    $BaseMem = memory_get_usage();

                    $Sheets = $Spreadsheet->Sheets();

                    //echo '---------------------------------'.PHP_EOL;
                    //echo 'Spreadsheets:'.PHP_EOL;
                    //print_r($Sheets);
                    //echo '---------------------------------'.PHP_EOL;
                    //echo '---------------------------------'.PHP_EOL;

                    foreach ($Sheets as $Index => $Name) {
                        //echo '---------------------------------'.PHP_EOL;
                        //echo '*** Sheet '.$Name.' ***'.PHP_EOL;
                        //echo '---------------------------------'.PHP_EOL;

                        $Time = microtime(true);

                        $Spreadsheet->ChangeSheet($Index);

                        foreach ($Spreadsheet as $Key => $Row) {
                            //echo $Key.': ';
                            if ($Row) {
                                //print_r($Row);
                                $allxlsdata[] = $Row;
                            } else {
                                var_dump($Row);
                            }
                            $CurrentMem = memory_get_usage();

                            //echo 'Memory: '.($CurrentMem - $BaseMem).' current, '.$CurrentMem.' base'.PHP_EOL;
                            //echo '---------------------------------'.PHP_EOL;

                            if ($Key && ($Key % 500 == 0)) {
                                //echo '---------------------------------'.PHP_EOL;
                                //echo 'Time: '.(microtime(true) - $Time);
                                //echo '---------------------------------'.PHP_EOL;
                            }
                        }

                        //	echo PHP_EOL.'---------------------------------'.PHP_EOL;
                        //echo 'Time: '.(microtime(true) - $Time);
                        //echo PHP_EOL;
                        //echo '---------------------------------'.PHP_EOL;
                        //echo '*** End of sheet '.$Name.' ***'.PHP_EOL;
                        //echo '---------------------------------'.PHP_EOL;
                    }
                } catch (Exception $E) {
                    echo $E->getMessage();
                }


                $this->qbank_model->import_question($allxlsdata);
            }
        } else {
            echo "Error: " . $_FILES["file"]["error"];
        }
        redirect('qbank');
    }

    function add_new() {

        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }
        $this->load->model('category', '', TRUE);
        $data['category'] = $this->category->category_dropdown();
        $this->load->model('difficult_level', '', TRUE);
        $data['difficult_level'] = $this->difficult_level->level_dropdown();
        if ($this->input->post('cid')) {
            $data['resultstatus'] = $this->qbank_model->add_question();
        }
        $data['title'] = "Add new question";
        $this->load->view('header', $data);

        $this->load->view('new_question', $data);
        $this->load->view('footer', $data);
    }

    function edit_question($id) {

        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }
        $this->load->model('category', '', TRUE);
        $data['category'] = $this->category->category_dropdown();
        $this->load->model('difficult_level', '', TRUE);
        $data['difficult_level'] = $this->difficult_level->level_dropdown();
        if ($this->input->post('cid')) {
            //print_r($_POST);
            $data['resultstatus'] = $this->qbank_model->update_question($id);
        }
        $data['result'] = $this->qbank_model->get_question($id);

        $data['title'] = "Edit question";
        $this->load->view('header', $data);

        $this->load->view('edit_question', $data);
        $this->load->view('footer', $data);
    }

    function remove_question($id) {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }
        $data['resultstatus'] = $this->qbank_model->remove_question($id);
        redirect('qbank', 'refresh');
    }

// get desired question for particular subject and difficulty level
    function get_level_question($difficulty_level, $category) {
        $this->db->where("cid", $category);
        $this->db->where("did", $difficulty_level);
        $query = $this->db->get("qbank");
        $num = $query->num_rows();
        echo $num;
    }

}
?>













